#import "RequestServerAPI.h"
#import "JsonUtil.h"
#import "PopUIManager.h"
#import "NetWorkManager.h"
#import "AuthCodeViewController.h"
#import "EditProfileViewController.h"
#import "LogInOutManager.h"
#import "MainViewCreater.h"
#import "UserDao.h"
#import "SetPasswordViewController.h"
#import "SelectSchoolViewController.h"
#import "YZRegInfoViewController.h"
#import "AddressListViewController.h"
#import "DynamicViewController.h"
#import "RegLoginViewController.h"

@implementation RequestServerAPI

/**
 requestType:(new)下拉刷新，(old)上拉加载更多
 */
+(BOOL)apiGetDynamicMessage:(DynamicViewController *)super_vc requestType:(NSString*)requestType baseId:(NSString *)base_id{
    
    NSString* classid = @"";
    NSString* uid = [NSString stringWithFormat:@"%ld",[[UserDao getInstance] getUserId]];
    NSString* utype = @"1";

    NSDictionary *temp_dict = [[NSDictionary alloc]initWithObjectsAndKeys:uid, @"uid", utype, @"utype",[[UserDao getInstance]getAccessToken], @"token", classid, @"classid",requestType,@"dir",base_id, @"baseid", nil];
    
    NSString *dataString = [JsonUtil convertDictionaryToJson:temp_dict];
    
    NSString *msg = @"USER_GET_DYNAMIC_MSGS";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if ([requestType isEqualToString:@"new"]) {
            [super_vc stopHeaderRefresh];
        }else if ([requestType isEqualToString:@"old"]){
            [super_vc stopFooterRefresh];
        }
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    if ([requestType isEqualToString:@"new"]) { //下拉 获取最新数据的回调
                        [super_vc setNewDataWithDict:dict];
                    }else if ([requestType isEqualToString:@"old"]){//上拉 加载更多旧数据的回调
                        [super_vc setOldDataWithDict:dict];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return YES;
}

//获取省市县镇地址列表
+(BOOL)apiGetAddressList:(YZRegInfoViewController *)super_vc {
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"type",@"0",@"id", nil]];
    
    NSString *msg = @"GET_ZONE_LIST";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
            NSLog(@"服务器返回StateCode:%ld", (long)responseCode);
            
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //获取到地址数据
                    //进入地址的选择列表
                    AddressListViewController *addressListViewController = [[AddressListViewController alloc]init];
                    addressListViewController.myDelegate = super_vc;
                    addressListViewController.addr_list = [dict objectForKey:@"data"];
                    [super_vc presentViewController:addressListViewController animated:YES completion:nil];
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return YES;
}

//园长提交申请
+(BOOL)apiCommitApply:(UIViewController *)super_vc schoolName:(NSString *)school_name schoolType:(NSString *)schoolNameType contactPerson:(NSString *)person phoneNumber:(NSString *)phone province:(NSString*)prov city:(NSString*)city country:(NSString*)country town:(NSString*)town{
    //此处的schooltype,如果是从系统列表中选取的话，需要置为1，自定义的置为2
    NSString *school = school_name;
    NSString *name = person;
    NSString *contact = phone;
    
    NSMutableDictionary* temp_dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:prov,@"province",city,@"city",country,@"county",school,@"school",schoolNameType,@"schooltype",name,@"name",contact,@"contact", nil];
    if (![town isEqualToString:@"null"]) {
        [temp_dict setValue:town forKey:@"town"];
    }
    NSString *dataString = [JsonUtil convertDictionaryToJson:temp_dict];
    
    NSString *msg = @"LEADER_REGISTER";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [super_vc.navigationController popViewControllerAnimated:YES];
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"server return dict:%@",dict);
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"SuccessApply";
                            
                            break;
                        }case 2:{
                            ret_msg= @"已经通过申请";
                            break;
                        }case 3:{
                            ret_msg= @"数据库操作失败";
                            break;
                        }case 4:{
                            ret_msg= @"已经有人申请，不可重复申请";
                            break;
                        }case 10021:{
                            ret_msg= @"未找到学校";
                            break;
                        }case 6:{
                            ret_msg= @"地区归属地不符";
                            break;
                        }case 7:{
                            ret_msg= @"数据库操作失败";
                            break;
                        }case 8:{
                            ret_msg= @"需完成所有地区选择";
                            break;
                        }case 9:{
                            ret_msg= @"地区不匹配";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"SuccessApply"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                    }else{
                        [popUIMgr popAlertView:nil title:@"提示" message:@"我们会审核您的信息" cancelButtonTitle:@"确定"];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return YES;
}

//发送镇的地址，获取镇上的幼儿园
+(BOOL)apiGetSchoolInTown:(YZRegInfoViewController *)super_vc townName:(NSString *)town{
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"type",town,@"id", nil]];
    
    NSString *msg = @"GET_ZONE_LIST";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"return: dict:  %@",dict);
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //进入幼儿园的选择列表
                    SelectSchoolViewController *selectSchoolViewController = [[SelectSchoolViewController alloc]init];
                    selectSchoolViewController.myDelegate = super_vc;
                    selectSchoolViewController.school_list = [dict objectForKey:@"data"];
                    [super_vc presentViewController:selectSchoolViewController animated:YES completion:nil];
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return YES;
}

+(BOOL)apiResetPassword:(UIViewController *)super_vc phoneNumber:(NSString *)number authCode:(NSString *)authCode password:(NSString *)pwd{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:pwd, @"newpwd",authCode,@"authcode",number,@"account",@"1",@"type", nil];

    NSLog(@"%@",dict);
    NSString *dataString = [JsonUtil convertDictionaryToJson:dict];
    
    NSString *msg = @"USER_DO_RESET_PWD";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [super_vc.navigationController popToRootViewControllerAnimated:YES];
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"Success";
                            break;
                        }case 10004:{
                            ret_msg= @"验证码失效";
                            break;
                        }case 10003:{
                            ret_msg= @"验证码不存在";
                            break;
                        }case 10005:{
                            ret_msg= @"数据库操作异常，请重试";
                            break;
                        }case 1001:{
                            ret_msg= @"用户不存在";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                    }else{
                        [popUIMgr popAlertView:nil title:@"提示" message:@"密码设置成功" cancelButtonTitle:@"确定"];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    
    return YES;
}

+(BOOL)apiTeacherRegister:(UIViewController *)super_vc userName:(NSString *) name password:(NSString*) pwd phoneNumber:(NSString*)number avatar:(NSString*)ava{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:name, @"name",pwd,@"pwd",number,@"phone", nil];
    if (ava != nil) {
        [dict setValue:ava forKey:@"avatar"];
    }
    NSLog(@"%@",dict);
    NSString *dataString = [JsonUtil convertDictionaryToJson:dict];
    
    NSString *msg = @"TEACHERS_REGISTER";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"Success";
                            NSLog(@"注册成功。");
                            //将服务器返回的字典重新传入教师园长登录接口，同步初始化内存、数据库。
                            NSDictionary* tempDict = [[dict objectForKey:@"data"] objectAtIndex:0]; //服务器返回的数据不是很舒服。
                            switch ([[LogInOutManager getInstance] teacherLoginCallback:tempDict]) {
                                    //1-进入主界面   2-修改园长号界面  3-老师加入学校界面
                                case 1:
                                    [popUIMgr removeWaitingUI];
                                    [super_vc.navigationController pushViewController:[[MainViewCreater getInstance]createMainView] animated:YES];
                                    break;
                                case 2:
                                    NSLog(@"2-修改园长号界面");
                                    break;
                                case 3:
                                    NSLog(@"3-老师加入学校界面");
                                    
                                    break;
                            };

                            break;
                        }case 2:{
                            ret_msg= @"手机格式错误";
                            break;
                        }case 3:{
                            ret_msg= @"手机号已经注册";
                            break;
                        }case 4:{
                            ret_msg= @"操作异常，请重试";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return true;
}


+(BOOL)apiTeacherRelogin:(UIViewController *)super_vc userId:(NSString *) userId userType:(NSString *) userType userToken:(NSString *) userToken {
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:userId, @"uid", userType, @"utype", userToken, @"token", nil]];
    
    NSString *msg = @"TEACHERS_RELOGIN";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"Success Login";
                            NSLog(@"重新登录成功。");
                            //将服务器返回的字典重新传入教师园长登录接口，同步初始化内存、数据库。
                            switch ([[LogInOutManager getInstance] teacherLoginCallback:dict]) {
                                    //1-进入主界面   2-修改园长号界面  3-老师加入学校界面
                                case 1:
                                    [popUIMgr removeWaitingUI];
                                    [super_vc.navigationController pushViewController:[[MainViewCreater getInstance]createMainView] animated:YES];
                                    break;
                                case 2:
                                    NSLog(@"2-修改园长号界面");
                                    break;
                                case 3:
                                    NSLog(@"3-老师加入学校界面");
                                    
                                    break;
                            };
                            break;
                        }case 2:{
                            ret_msg= @"用户名没找到";
                            break;
                        }case 3:{
                            ret_msg= @"用户名与密码不匹配";
                            break;
                        }case 4:{
                            ret_msg= @"账号已经停用";
                            break;
                        }case 10010:{
                            ret_msg= @"你的账号已经在其他地方登陆，请重新登陆账号。";
                            //清空内存中的UserInfo数据，重置数据库中的hasLogin标志位是0，跳转到注册登陆界面，重新登陆
                            [[UserDao getInstance]logOut];
                            RegLoginViewController *reg = [[RegLoginViewController alloc]init];
                            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:reg];
                            UIWindow *window = [[UIApplication sharedApplication]keyWindow];
                            window.rootViewController = nav;
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success Login"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return true;
}

/**
 教师加入学校园所
 */
+(BOOL)apiJoinSchool:(UIViewController*)super_vc schoolCode:(NSString *)school_code{
    NSString* uid = [NSString stringWithFormat:@"%ld",[[UserDao getInstance]getUserId]];
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:uid, @"uid",school_code,@"schoolcode", nil]];
    
    NSString *msg = @"ADD_ME_TO_SCHOOL";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
            NSLog(@"服务器返回StateCode:%ld", (long)responseCode);
            
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"SuccessJoin";
                            //教师加入学校，本地的内存，数据库的同步更新数据
                            if ([[LogInOutManager getInstance]joinSchool:dict schoolCode:school_code]) {
                                [super_vc presentViewController:[[MainViewCreater getInstance]createMainView] animated:YES completion:nil];
                            }
                            break;
                        }case 2:{
                            ret_msg= @"错误，请重试";
                            break;
                        }case 3:{
                            ret_msg= @"用户不存在";
                            break;
                        }case 4:{
                            ret_msg= @"学校不存在";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"SuccessJoin"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return true;
}


/*
 教师登录
 */
+(BOOL)apiTeacherLogin:(UIViewController *)super_vc account:(NSString *) account password:(NSString *)psw{
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:account, @"username",psw,@"psw", nil]];
    
    NSString *msg = @"TEACHERS_LOGIN";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
            NSLog(@"服务器返回StateCode:%ld", (long)responseCode);
            
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"Success Login";
                            switch ([[LogInOutManager getInstance] teacherLoginCallback:dict]) {
                                //1-进入主界面   2-修改园长号界面  3-老师加入学校界面
                                case 1:	
                                    [popUIMgr removeWaitingUI];
                                    [super_vc.navigationController pushViewController:[[MainViewCreater getInstance]createMainView] animated:YES];
                                    break;
                                case 2:
                                    NSLog(@"2-修改园长号界面");
                                    break;
                                case 3:
                                    NSLog(@"3-老师加入学校界面");
                                    
                                    break;
                            };
                            break;
                        }case 2:{
                            ret_msg= @"用户名没找到";
                            break;
                        }case 3:{
                            ret_msg= @"用户名与密码不匹配";
                            break;
                        }case 4:{
                            ret_msg= @"账号已经停用";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success Login"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return true;
}


/**
 手机注册时，点击收取验证码，发送手机号到服务器验证。
 codetype: 1-register   2-forget password   3-change phone number
 flag: 1-register   2-forget password
 */
+(BOOL) apiRegisterPhone:(UIViewController *)super_vc phoneNumber:(NSString*)number type:(NSString*)type codetype:(NSString*)codetype superFlag:(int) flag{
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:type, @"type", number, @"account", codetype, @"codetype", nil]];
    
    NSString *msg = @"USER_GET_AUTH_CODE";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 10007:{
                            ret_msg= @"该手机号已经注册过";
                            break;
                        }case 1:{
                            ret_msg= @"手机号格式错误";
                            break;
                        }case 3:{
                            ret_msg= @"Success";
                            break;
                        }case 4:{
                            ret_msg= @"数据库操作异常，请稍后再试";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                        [popUIMgr removeWaitingUI];
                        return;
                    }else{
                        if (flag == 1) {
                            AuthCodeViewController *authCodeViewController = [[AuthCodeViewController alloc]init];
                            authCodeViewController.phoneNum = number;
                            [super_vc.navigationController pushViewController:authCodeViewController animated:YES];
                        }else if (flag == 2) {
                            SetPasswordViewController *setPwdViewController = [[SetPasswordViewController alloc]init];
                            setPwdViewController.phoneNumber = number;
                            [super_vc.navigationController pushViewController:setPwdViewController animated:YES];
                        
                        }

                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return true;
}

+(BOOL) apiValidCode:(UIViewController *)super_vc phoneNumber:(NSString*) number authCode:(NSString *)auth_code{
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:@"2", @"type", auth_code, @"authcode", number, @"account", nil]];
    
    NSString *msg = @"USER_CHECK_REGISTER_AUTHCODE";
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:super_vc.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 10007:{
                            ret_msg= @"该手机号已经注册过";
                            break;
                        }case 10003:{
                            ret_msg= @"验证码错误";
                            break;
                        }case 10006:{
                            ret_msg= @"手机号格式错误";
                            break;
                        }case 10004:{
                            ret_msg= @"验证码失效";
                            break;
                        }case 1001:{
                            ret_msg= @"用户类型错误";
                            break;
                        }case 1:{
                            ret_msg= @"Success";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                        [popUIMgr removeWaitingUI];
                        return;
                    }else{
                        EditProfileViewController *epViewController = [[EditProfileViewController alloc] init];
                        epViewController.phoneNumber = number;
                        [super_vc.navigationController pushViewController:epViewController animated:YES];
//                        [super_vc presentViewController:epViewController animated:YES completion:nil];
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];
    return true;
}

@end
